package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.MoneySettlement;
import com.ruoyi.common.core.domain.entity.RestaurantBalanceDetail;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.MoneySettlementMapper;
import com.ruoyi.system.mapper.RestaurantBalanceDetailMapper;
import com.ruoyi.system.service.IMoneySettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author wlmes
 * @date 2023-12-18
 */
@Service
public class MoneySettlementServiceImpl implements IMoneySettlementService {
    @Resource
    private MoneySettlementMapper moneySettlementMapper;

    @Resource
    RestaurantBalanceDetailMapper restaurantBalanceDetailMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public MoneySettlement selectMoneySettlementById(Long id) {
        return moneySettlementMapper.selectMoneySettlementById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param moneySettlement 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<MoneySettlement> selectMoneySettlementList(MoneySettlement moneySettlement) {
        return moneySettlementMapper.selectMoneySettlementList(moneySettlement);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param moneySettlement 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertMoneySettlement(MoneySettlement moneySettlement) {
        if (StringUtils.isNull(moneySettlement)) {
            throw new BaseException("新增【请填写功能名称】失败！");
        }
        moneySettlement.setCreateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        moneySettlement.setCreateBy(String.valueOf(user.getUserId()));
        return moneySettlementMapper.insertMoneySettlement(moneySettlement);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param moneySettlement 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateMoneySettlement(MoneySettlement moneySettlement) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        moneySettlement.setUpdateBy(String.valueOf(user.getUserId()));
        return moneySettlementMapper.updateMoneySettlement(moneySettlement);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteMoneySettlementByIds(Long[] ids) {
        if (StringUtils.isNull(ids)) {
            throw new BaseException("需要删除的【请填写功能名称】ID为空！");
        }
        return moneySettlementMapper.deleteMoneySettlementByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteMoneySettlementById(Long id) {
        if (StringUtils.isNull(id)) {
            throw new BaseException("需要删除的【请填写功能名称】ID为空！");
        }
        return moneySettlementMapper.deleteMoneySettlementById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveNewMoneySettlement(MoneySettlement moneySettlement) {
        moneySettlement.setSettlementTime(new Date());

        String area = "";
        if (moneySettlement != null && StringUtils.isNotBlank(moneySettlement.getArea())) {
            area = moneySettlement.getArea();
        }
        if (StringUtils.isBlank(area)) {
            area = SecurityUtils.getUserLocation();
        }
        if (StringUtils.isBlank(area) || area.equals("全部")) {
            throw new BaseException("登录用户的园区为全部,无法新增");
        }

        moneySettlement.setArea(area);

        //查询重复之后再新增
        List<MoneySettlement> list = moneySettlementMapper.selectMoneySettlementList(moneySettlement);
        if (!CollectionUtils.isEmpty(list)) {
            throw new BaseException("该园区今日已存在结算单,无法新增" + area);
        }

        //查询消费明细
        RestaurantBalanceDetail query = new RestaurantBalanceDetail();
        query.setLocation(moneySettlement.getArea());
        query.setToday(moneySettlement.getSettlementTime());
        List<RestaurantBalanceDetail> balanceDetails = restaurantBalanceDetailMapper.selectRestaurantBalanceDetailList(query);

        BigDecimal staffUseTotal = BigDecimal.ZERO;
        BigDecimal householdUseTotal = BigDecimal.ZERO;
        BigDecimal staffCaseTotal = BigDecimal.ZERO;
        BigDecimal householdCaseTotal = BigDecimal.ZERO;
        BigDecimal staffCardTotal = BigDecimal.ZERO;
        BigDecimal householdCardTotal = BigDecimal.ZERO;
        BigDecimal staffBuckleTotal = BigDecimal.ZERO;
        BigDecimal householdBuckleTotal = BigDecimal.ZERO;

        //按园区分组
        List<RestaurantBalanceDetail> staffList = new ArrayList<>();//员工
        List<RestaurantBalanceDetail> householdList = new ArrayList<>();//住户
        for (RestaurantBalanceDetail balanceDetail : balanceDetails) {
            if (StringUtils.isNotBlank(balanceDetail.getLocation()) && (balanceDetail.getLocation().equals("一区") || balanceDetail.getLocation().equals("二区"))) {
                staffList.add(balanceDetail);
            } else {
                householdList.add(balanceDetail);
            }
        }

        //分别统计 员工
        for (RestaurantBalanceDetail restaurantBalanceDetail : staffList) {
            if (StringUtils.isNotBlank(restaurantBalanceDetail.getType()) && restaurantBalanceDetail.getType().equals("用户消费")) {
                staffUseTotal = staffUseTotal.add(restaurantBalanceDetail.getBalance() == null ? BigDecimal.ZERO : restaurantBalanceDetail.getBalance());
            }
            if (StringUtils.isNotBlank(restaurantBalanceDetail.getReason()) && restaurantBalanceDetail.getReason().equals("现金充值")) {
                staffCaseTotal = staffCaseTotal.add(restaurantBalanceDetail.getBalance() == null ? BigDecimal.ZERO : restaurantBalanceDetail.getBalance());
            }
            if (StringUtils.isNotBlank(restaurantBalanceDetail.getReason()) && restaurantBalanceDetail.getReason().equals("信用卡充值")) {
                staffCardTotal = staffCardTotal.add(restaurantBalanceDetail.getBalance() == null ? BigDecimal.ZERO : restaurantBalanceDetail.getBalance());
            }
            if (StringUtils.isNotBlank(restaurantBalanceDetail.getReason()) && restaurantBalanceDetail.getReason().equals("补缴扣费")) {
                staffBuckleTotal = staffBuckleTotal.add(restaurantBalanceDetail.getBalance() == null ? BigDecimal.ZERO : restaurantBalanceDetail.getBalance());
            }
        }

        //分别统计 住户
        for (RestaurantBalanceDetail restaurantBalanceDetail : householdList) {
            if (StringUtils.isNotBlank(restaurantBalanceDetail.getType()) && restaurantBalanceDetail.getType().equals("用户消费")) {
                householdUseTotal = householdUseTotal.add(restaurantBalanceDetail.getBalance() == null ? BigDecimal.ZERO : restaurantBalanceDetail.getBalance());
            }
            if (StringUtils.isNotBlank(restaurantBalanceDetail.getReason()) && restaurantBalanceDetail.getReason().equals("现金充值")) {
                householdCaseTotal = householdCaseTotal.add(restaurantBalanceDetail.getBalance() == null ? BigDecimal.ZERO : restaurantBalanceDetail.getBalance());
            }
            if (StringUtils.isNotBlank(restaurantBalanceDetail.getReason()) && restaurantBalanceDetail.getReason().equals("信用卡充值")) {
                householdCardTotal = householdCardTotal.add(restaurantBalanceDetail.getBalance() == null ? BigDecimal.ZERO : restaurantBalanceDetail.getBalance());
            }
            if (StringUtils.isNotBlank(restaurantBalanceDetail.getReason()) && restaurantBalanceDetail.getReason().equals("补缴扣费")) {
                householdBuckleTotal = householdBuckleTotal.add(restaurantBalanceDetail.getBalance() == null ? BigDecimal.ZERO : restaurantBalanceDetail.getBalance());
            }
        }

        moneySettlement.setStaffUseTotal(staffUseTotal);
        moneySettlement.setHouseholdUseTotal(householdUseTotal);
        moneySettlement.setStaffCaseTotal(staffCaseTotal);
        moneySettlement.setHouseholdCaseTotal(householdCaseTotal);
        moneySettlement.setStaffCardTotal(staffCardTotal);
        moneySettlement.setHouseholdCardTotal(householdCardTotal);
        moneySettlement.setStaffBuckleTotal(staffBuckleTotal);
        moneySettlement.setHouseholdBuckleTotal(householdBuckleTotal);
        moneySettlement.setCreateBy(SecurityUtils.getNickName());
        moneySettlement.setCreateTime(new Date());
        moneySettlement.setBusinessNum(BaseController.createBusinessNum("MSD"));
        moneySettlementMapper.insertMoneySettlement(moneySettlement);
    }
}
